/*
Automatic clinic tests using a robot arm.
Copyright (C) 2010  CIC-CPC, CHU Gabriel Montpied
All other trademarks, logos and copyrights are the property
of their respective owners.

This file is part of cpc-painbot.

cpc-painbot is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

cpc-painbot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with cpc-painbot.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "brasgraphique.h"

#include <QDebug>

#include "positionbras.h"

BrasGraphique::BrasGraphique(QGraphicsItem * parent)
:QGraphicsItemGroup(parent),
bras_(new QGraphicsRectItem(this)),
avantbras_(new QGraphicsRectItem(bras_)),
tete_(new QGraphicsEllipseItem(avantbras_)),
direction_(new QGraphicsLineItem(tete_))
{
    QRectF rect;
    bras_->setRect(-30, 0, 60, PositionBras::TailleBras);
    rect = bras_->rect();
    bras_->setTransformOriginPoint(rect.topLeft().x() + rect.width() / 2, rect.topLeft().y());

    // x = (bras.largeur - avantbras.largeur) / 2
    avantbras_->setRect(rect.topLeft().x() + (rect.width() - 40) / 2, PositionBras::TailleBras,
                            40, PositionBras::TailleAvantBras);

    rect = avantbras_->rect();
    avantbras_->setTransformOriginPoint(rect.topLeft().x() + rect.width() / 2, rect.topLeft().y());

    // x = avantbras.haut_gauche + (avantbras.largeur - tete.largeur) / 2
    tete_->setRect(rect.topLeft().x() + (rect.width() - 40) / 2, PositionBras::TailleTotale - 20, 40, 40);
    tete_->setFlag(QGraphicsItem::ItemIsSelectable, true);
    tete_->setFlag(QGraphicsItem::ItemIsMovable, true);
    rect = tete_->rect();

    // Ligne (avantbras.largeur / 2, bout de l'avantbras) -> (même x, bout de la tête + un offset)
    direction_->setLine(rect.topLeft().x() + rect.width() / 2, rect.bottomLeft().y() - rect.height() / 2,
                            rect.topLeft().x() + rect.width() / 2, rect.bottomLeft().y() + rect.height() / 2);

    direction_->setTransformOriginPoint(direction_->line().p1());
}

void BrasGraphique::majPosition(const PositionBras &pos)
{
    qreal angle_bras = pos.angle() + pos.angleBras();
    qreal angle_avantbras = pos.coude();

    qDebug() << "Angle du bras : rad =" << angle_bras << ", deg =" << (angle_bras * 180/M_PI)
             << "Angle bras/avant-bras : rad =" << angle_avantbras << ", deg =" << (angle_avantbras * 180/M_PI);
    bras_->setRotation(angle_bras * 180/M_PI);
    avantbras_->setRotation(angle_avantbras * 180/M_PI);

    direction_->setRotation(pos.r());
}
